package cn.chengpengper.psi.action;

import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;

public class GetMethodParamAction extends AnAction {
    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        PsiElement psiElement = e.getData(CommonDataKeys.PSI_ELEMENT);
        if(!(psiElement instanceof PsiMethod)){
            return;
        }
        StringBuilder result = new StringBuilder();
        Project project = e.getProject();
        PsiMethod psiMethod = (PsiMethod) psiElement;
        for (PsiParameter parameter : psiMethod.getParameterList().getParameters()) {
            String canonicalText = parameter.getType().getCanonicalText();
            PsiClass aClass = JavaPsiFacade.getInstance(project).findClass(canonicalText, GlobalSearchScope.allScope(project));
            for (PsiField field : aClass.getAllFields()) {
                result.append(field.getName()).append("<br>");
            }
        }
        Notifications.Bus.notify(new Notification("Print", "PSIDemo", result.toString(), NotificationType.INFORMATION), e.getProject());
    }
}
